﻿using DecisionsFramework.Design.Properties;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;

namespace Decisions.HL7.DataTypes
{
    [DataContract]
    public class ZSegment : AbstractSegment
    {
        [DataMember]
        public string SegmentName { get; set; }

        [DataMember]
        public int NumberFieldsToOutput { get; set; }

        [DataMember]
        [PropertyClassification("Value 1", 1)]
        public string Value1 { get; set; }

        [DataMember]
        [PropertyClassification("Value 2", 2)]
        public string Value2 { get; set; }

        [DataMember]
        [PropertyClassification("Value 3", 3)]
        public string Value3 { get; set; }

        [DataMember]
        [PropertyClassification("Value 4", 4)]
        public string Value4 { get; set; }

        [DataMember]
        [PropertyClassification("Value 5", 5)]
        public string Value5 { get; set; }

        [DataMember]
        [PropertyClassification("Value 6", 6)]
        public string Value6 { get; set; }

        [DataMember]
        [PropertyClassification("Value 7", 7)]
        public string Value7 { get; set; }

        [DataMember]
        [PropertyClassification("Value 8", 8)]
        public string Value8 { get; set; }

        [DataMember]
        [PropertyClassification("Value 9", 9)]
        public string Value9 { get; set; }

        [DataMember]
        [PropertyClassification("Value 10", 10)]
        public string Value10 { get; set; }

        [DataMember]
        [PropertyClassification("Value 11", 11)]
        public string Value11 { get; set; }

        [DataMember]
        [PropertyClassification("Value 12", 12)]
        public string Value12 { get; set; }

        [DataMember]
        [PropertyClassification("Value 13", 13)]
        public string Value13 { get; set; }

        [DataMember]
        [PropertyClassification("Value 14", 14)]
        public string Value14 { get; set; }

        [DataMember]
        [PropertyClassification("Value 15", 15)]
        public string Value15 { get; set; }

        [DataMember]
        [PropertyClassification("Value 16", 16)]
        public string Value16 { get; set; }

        [DataMember]
        [PropertyClassification("Value 17", 17)]
        public string Value17 { get; set; }

        [DataMember]
        [PropertyClassification("Value 18", 18)]
        public string Value18 { get; set; }

        [DataMember]
        [PropertyClassification("Value 19", 19)]
        public string Value19 { get; set; }

        [DataMember]
        [PropertyClassification("Value 20", 20)]
        public string Value20 { get; set; }

        [DataMember]
        [PropertyClassification("AdditionalData", 21)]
        public string[] AdditionalData { get; set; }


        internal override void ConsumeTextSegment(string segmentTextData, HL7ParserOptions options)
        {
            string[] segmentTextParts = segmentTextData.Split(new char[] { options.FieldSep }, StringSplitOptions.None);
            SegmentName = segmentTextParts[0];
            Value1 = segmentTextParts[1];
            if (segmentTextParts.Length > 2)
                Value2 = segmentTextParts[2];
            if (segmentTextParts.Length > 3) 
                Value3 = segmentTextParts[3];
            if (segmentTextParts.Length > 4)
                Value4 = segmentTextParts[4];
            if (segmentTextParts.Length > 5) 
                Value5 = segmentTextParts[5];
            if (segmentTextParts.Length > 6) 
                Value6 = segmentTextParts[6];
            if (segmentTextParts.Length > 7) 
                Value7 = segmentTextParts[7];
            if (segmentTextParts.Length > 8) 
                Value8 = segmentTextParts[8];
            if (segmentTextParts.Length > 9) 
                Value9 = segmentTextParts[9];
            if (segmentTextParts.Length > 10) 
                Value10 = segmentTextParts[10];
            if (segmentTextParts.Length > 11) 
                Value11 = segmentTextParts[11];
            if (segmentTextParts.Length > 12) 
                Value12 = segmentTextParts[12];
            if (segmentTextParts.Length > 13) 
                Value13 = segmentTextParts[13];
            if (segmentTextParts.Length > 14) 
                Value14 = segmentTextParts[14];
            if (segmentTextParts.Length > 15) 
                Value15 = segmentTextParts[15];
            if (segmentTextParts.Length > 16) 
                Value16 = segmentTextParts[16];
            if (segmentTextParts.Length > 17) 
                Value17 = segmentTextParts[17];
            if (segmentTextParts.Length > 18) 
                Value18 = segmentTextParts[18];
            if (segmentTextParts.Length > 19) 
                Value19 = segmentTextParts[19];
            if (segmentTextParts.Length > 20) 
                Value20 = segmentTextParts[20];

            if (segmentTextParts.Length > 21) {
                List<string> extraData = new List<string>();
                for (int i = 21; i < segmentTextParts.Length; i++) {
                    extraData.Add(segmentTextParts[i]);
                }
                AdditionalData = extraData.ToArray();
            }

        }

        public string GetER7(HL7ParserOptions options)
        {
            List<string> segmentParts = new List<string>();
            segmentParts.Add(SegmentName);
            if (NumberFieldsToOutput == 0) {
                NumberFieldsToOutput = -1;
            }

            segmentParts.Add(Value1);

            if (AdditionalData != null && AdditionalData.Length > 0) {
                foreach (string eachExtraData in AdditionalData) {
                    segmentParts.Add(eachExtraData);
                }
            }

            // Now truncate.
            if (NumberFieldsToOutput > 0) {
                string[] newList = segmentParts.Take(NumberFieldsToOutput).ToArray();
                segmentParts.Clear();
                segmentParts.AddRange(newList);
            }

            return string.Join("" + options.FieldSep, segmentParts.ToArray());
        }
    }
}
